package com.glory.dmegcpda.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import com.glory.dmegcpda.R;
import com.glory.dmegcpda.adapter.LookPDFAdapter;
import com.glory.dmegcpda.utils.PdfUtils;
import com.glory.dmegcpda.utils.VideoModel;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class LookPDFActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView mList;
    private Button btn_close; //关闭识别结果框的按钮
    private ListView lv; //显示pdf文件集合控件
    private String[] strings; //pdf文件数组
    private List<VideoModel> list;//pdf文件集合
    LookPDFAdapter madapter;
    private PDFView pdfView;
    private BottomSheetDialog bottomSheetDialog;
    private TextView tvNull;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_pdf);
        initview();
        initData();

    }

    /**
     * 初始化数据
     */
    private void initData() {
        strings = PdfUtils.getString();
        list = new ArrayList<VideoModel>();
        madapter=new LookPDFAdapter(this,list);
        mList.setAdapter(madapter);
        madapter.notifyDataSetChanged();
        if (strings != null) {
            mList.setVisibility(View.VISIBLE);
            tvNull.setVisibility(View.GONE);
            for (int i = 0; i < strings.length; i++) {
                VideoModel model = new VideoModel();
                String url = strings[i];
                model.setName(url.substring(url.lastIndexOf('/') + 1));
                model.setPath(url);
                list.add(model);
            }
        }else {
            mList.setVisibility(View.GONE);
            tvNull.setVisibility(View.VISIBLE);
        }
    }
    private void initview() {
        mList=findViewById(R.id.ac_look_pdf);
        tvNull=findViewById(R.id.tvNull);
        mList.setOnItemClickListener(this);
         bottomSheetDialog=new BottomSheetDialog(this);

         bottomSheetDialog.setContentView(R.layout.activity_pdf);

         pdfView=bottomSheetDialog.findViewById(R.id.pdfView);
        //给布局设置透明背景色
        bottomSheetDialog.getDelegate().findViewById(R.id.design_bottom_sheet)
                .setBackgroundColor(getResources().getColor(android.R.color.transparent));
        if (PdfUtils.getString() != null){readPdfContent(PdfUtils.getString()[0]);}

    }
    /**
     * 读取Pdf文件的内容
     * @param path :文件地址
     */
    public void readPdfContent(String path){
        /*pdfView.fromUri(Uri)
        or
        pdfView.fromFile(File)
        or
        pdfView.fromBytes(byte[])
        or
        pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
        or
        pdfView.fromSource(DocumentSource)
        fromAsset
        or*/
        Log.e("pathload",path.toString());
        pdfView.fromFile(new File(path))
                .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .defaultPage(0)
                // allows to draw something on the current page, usually visible in the middle of the screen
                /*.onDraw(onDrawListener)
                // allows to draw something on all pages, separately for every page. Called only for visible pages
                .onDrawAll(onDrawListener)
                .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
                .onPageChange(onPageChangeListener)
                .onPageScroll(onPageScrollListener)
                .onError(onErrorListener)
                .onPageError(onPageErrorListener)
                .onRender(onRenderListener) // called after document is rendered for the first time
                // called on single tap, return true if handled, false to toggle scroll handle visibility
                .onTap(onTapListener)
                .onLongPress(onLongPressListener)*/
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
                /*.linkHandler(DefaultLinkHandler)
                .pageFitPolicy(FitPolicy.WIDTH)*/
                .pageSnap(true) // snap pages to screen boundaries
                .pageFling(false) // make a fling change only a single page like ViewPager
                .nightMode(false) // toggle night mode
                .load();
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        if (PdfUtils.getString() != null) {
            Log.e("pduss", PdfUtils.getString()[position].toString());
            String path = PdfUtils.getString()[position];
            Intent intent = new Intent(this, PdfActivity.class);
            intent.putExtra("path", path);
            startActivity(intent);
        }else {
            Toast.makeText(LookPDFActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
            list.remove(position);
            madapter.notifyDataSetChanged();
        }
    }
}
